package cn.edu.ujn.lab3.service;

import java.util.List;

//import org.apache.commons.lang3.StringUtils;
//import org.omg.CORBA.PRIVATE_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mysql.jdbc.StringUtils;

import cn.edu.ujn.lab3.dao.Customer;
import cn.edu.ujn.lab3.dao.CustomerMapper;
import cn.edu.ujn.lab3.util.Page;
@Service("customerService")
@Transactional
public class CustomerServiceImpl implements CustomerService {
	@Autowired
	private CustomerMapper customerDao;
	
	/*
	 * 查询
	 * */
	@Override
	public Page<Customer> findCustomerList(Integer page, Integer rows, String custName, String custSource,
			String custIndustry, String custLevel) {
		// TODO Auto-generated method stub
		// 创建客户对象
		Customer customer = new Customer();
		// 判断客户名称
		if (!StringUtils.isNullOrEmpty(custName)) {
			customer.setCustName(custName);
		}
//		if(StringUtils.isNotBlank(custName)) {
//			customer.setCustName(custName);
//		}
		// 判断客户信息来源
		if (!StringUtils.isNullOrEmpty(custSource)) {
			customer.setCustSource(custSource);
		}
		// 判断客户所属行业

		if (!StringUtils.isNullOrEmpty(custIndustry)) {
			customer.setCustIndustry(custIndustry);
		}
		// 判断客户级别
		if (!StringUtils.isNullOrEmpty(custLevel)) {
			customer.setCustLevel(custLevel);
		}
		// 当前页
		customer.setStart((page - 1) * rows);
		// 每页数
		customer.setRows(rows);
		// 查询客户列表
		List<Customer> customers = customerDao.selectCustomerList(customer);
		// 查询客户列表总记录数
		Integer count = customerDao.selectCustomerListCount(customer);
		// 创建Page返回对象
		Page<Customer> result = new Page<>();
		result.setPage(page);
		result.setRows(customers);
		result.setSize(rows);
		result.setTotal(count);
		return result;

	}

	@Override
	public int createCustomer(Customer customer) {
		// TODO Auto-generated method stub
		return customerDao.createCustomer(customer);
	}

	@Override
	public Customer getCustomerById(Integer id) {
		// TODO Auto-generated method stub
		Customer customer = customerDao.getCustomerById(id);
		return customer;
	}

	@Override
	public int deleteCustomer(Integer id) {
		// TODO Auto-generated method stub
		return customerDao.deleteCustomer(id);
	}

	/*
	 * 更新
	 */
	@Override
	public int updateCustomer(Customer customer) {
		// TODO Auto-generated method stub
		System.out.println(customer.getCustAddress());
		System.out.println(customer.getCustId());
		return customerDao.updateCustomer(customer);
	}

}
